<template>
  <div class="subjects-manage">
    <el-card>
      <!-- 搜索框内容 -->
      <div class="search">
        <!-- 搜索框 -->
        <el-form label-width="80px" class="label_form">
          <el-form-item label="学科名称">
            <el-input
              type="text"
              autocomplete="off"
              class="input_inner"
              v-model="queryInfo.subjectName"
            ></el-input>
            <el-button class="clearbtn" @click="clearInput">清除</el-button>
            <el-button type="primary" @click="getSubjectsList">搜索</el-button>
            <el-button
              type="success"
              icon="el-icon-edit"
              class="successbtn"
              @click="addSubjectsDialogShow = true"
              >新增学科</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <!-- 提示框 -->
      <el-alert
        :title="`数据一共${counts}条`"
        type="info"
        show-icon
        :closable="false"
      >
      </el-alert>
      <!-- 数据表单 -->
      <el-table
        fit
        :data="subjectslist"
        class="table_data"
        v-loading="listLoading"
        style="width:100%"
        element-loading-text="给我一点时间"
      >
        <el-table-column align="center" type="index" label="序号" width="50">
        </el-table-column>
        <el-table-column align="center" label="学科名称" width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.subjectName }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="创建者" width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.username }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="创建日期" width="150">
          <template slot-scope="scope">
            <span>{{ scope.row.addDate | parseTimeByString }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="前台是否显示" width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.isFrontDisplay == 1 ? '是' : '否' }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="二级目录" width="80">
          <template slot-scope="scope">
            <span>{{ scope.row.twoLevelDirectory }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="标签" width="60">
          <template slot-scope="scope">
            <span>{{ scope.row.tags }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="数目数量" width="80">
          <template slot-scope="scope">
            <span>{{ scope.row.totals }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <!-- <el-button
              class="subjectsOp"
              @click="$router.push({ path: '/subjects/directorys' })"
              >学科分类</el-button
            > -->
            <span class="operationText" :id="scope.row.id" @click="toDirectory(scope.row.id,scope.row.subjectName)">学科目录</span>
            <!-- <el-button
              class="subjectsOp"
              @click="
                $router.push({
                  path: '/subjects/tags',
                  query: { id: scope.row.id }
                })
              "
              >学科标签</el-button
            > -->
            <span class="operationText" @click="toTags(scope.row.id,scope.row.subjectName)">学科标签</span>
            <el-button
              class="subjectsOp"
              @click="editSubjectNameEvent(scope.row)"
              >修改</el-button
            >
            <el-button class="subjectsOp" @click="removeSubject(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <el-pagination
        class="page-num"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="counts"
      >
      </el-pagination>
      <!-- 添加学科对话框 -->
      <el-dialog
        title="新增学科"
        :visible.sync="addSubjectsDialogShow"
        width="30%"
      >
        <subjects-add
          v-if="addSubjectsDialogShow"
          @close="addSubjectsDialogShow = false"
          @addSubjects="getSubjectsList"
        ></subjects-add>
      </el-dialog>
      <!-- 修改学科对话框 -->
      <el-dialog
        title="修改学科"
        :visible.sync="correctSubjectsDialogShow"
        width="30%"
        v-model="queryInfo.subjectName"
      >
        <subjects-add
          v-if="correctSubjectsDialogShow"
          @close="correctSubjectsDialogShow = false"
          @updateSubjects="getSubjectsList"
          :correctSubjects="correctSubjects"
        ></subjects-add>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/subjects'
import SubjectsAdd from '../components/subjects-add'
export default {
  name: 'SubjectsManage',
  data() {
    return {
      correctSubjects: null, // 需要修改的学科名称
      queryInfo: {
        page: 1,
        pagesize: 10,
        subjectName: '' // 学科名称
      }, // 获取数据参数
      subjectslist: [], // 获取学科数据列表数据
      listLoading: false, // 加载提示
      counts: 0, // 数据列表总数
      addSubjectsDialogShow: false, // 控制添加学科对话框的显示和隐藏
      correctSubjectsDialogShow: false // 控制修改学科对话框的显示和隐藏
    }
  },
  components: {
    SubjectsAdd
  },
  created() {
    this.getSubjectsList()
  },
  methods: {
    async getSubjectsList() {
      try {
        const { data } = await list(this.queryInfo)
        console.log(data)
        this.subjectslist = data.items
        this.counts = data.counts
        this.queryInfo.page = parseInt(data.page)
        this.queryInfo.pagesize = parseInt(data.pagesize)
      } catch (error) {
        this.$message.error('获取数据失败，请稍后重试！')
      }
    },
    // 监听pagesize的变化
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getSubjectsList()
    },
    // 监听page的变化
    handleCurrentChange(newPage) {
      this.queryInfo.page = newPage
      this.getSubjectsList()
    },
    clearInput() {
      this.queryInfo.subjectName = ''
      this.getSubjectsList()
    },
    // 点击删除按钮，删除当前学科所在行
    async removeSubject(row) {
      console.log(row)
      const confirmResult = await this.$confirm(
        '此操作将永久删除该学科, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      remove(row).then(res => {
        // console.log(res);
        if (res.status !== 200) {
          return this.$message.error('删除数据失败')
        }
        this.$message.success('删除数据成功')
        this.getSubjectsList()
      })
    },
    editSubjectNameEvent(item) {
      this.correctSubjectsDialogShow = true
      this.correctSubjects = item
      // console.log( this.correctSubjects);
    },
    // 点击标签分类 跳转到标签二级
    toTags (id, name) {
      this.$router.push({
        path: '/subjects/tags',
        query: { id: id, name: name }
      })
    },
    toDirectory (id, name) {
      console.log(id)
      this.$router.push({
        path: '/subjects/directorys',
        query: { id: id, name: name }
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.subjects-manage {
  .input_inner {
    width: 200px;
    height: 32px;
  }
  .clearbtn {
    margin-left: 8px;
  }
  .successbtn {
    float: right;
  }
  .table_data {
    margin-top: 10px;
    .subjectsOp {
      padding: 0 5px;
      color: rgb(0, 153, 255);
      border: none;
    }
  }
  .page-num {
    float: right;
    margin: 10px 5px;
  }
  .dialog-footer {
    text-align: right;
  }
  .operationText {
    color: #409EFF;
    margin: 0 15px;
    cursor: pointer;
}
}
</style>
